package src.ee.ioc.smaug.ui.evolver;

import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;

import src.ee.ioc.smaug.ui.WMain;
import src.ee.ioc.smaug.world.Enviroment;
import src.ee.ioc.smaug.world.evolution.*;

import javax.swing.BorderFactory;

import java.awt.Color;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;

import src.ee.ioc.smaug.parser.*;

public class PControlsEvolver extends JPanel {
    JButton bBrowse = new JButton("Browse results");

    JButton bEvolve = new JButton("Evolve");

    JButton bStat = new JButton("Statistics");

    PControlsEvolver() {
        super(null);
        this.setBorder(BorderFactory.createLineBorder(Color.gray));

        bBrowse.setBounds(410, 5, 110, 30);
        bBrowse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if (Desktop.isDesktopSupported()) {
                    Desktop d = Desktop.getDesktop();
                    try {
                        d.browse(new URI(EnvDescription.resFile.toURI().toString()));
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } catch (URISyntaxException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
        add(bBrowse);

        bStat.setBounds(120, 5, 110, 30);
        bStat.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {

                String input;
                int inp = 0;

                boolean correct = false;
                while (!correct) {
                    input = JOptionPane.showInputDialog("Run times:");
                    if ((input == null)) {
                        correct = true;
                        break;
                    }
                    if ((input.length() > 0)) {
                        inp = Integer.parseInt(input);
                        if ((inp >= 1) && (inp <= 1000)) {
                            correct = true;
                            EnvDescription.action = EnvDescription.RUN_STAT;
                            EnvDescription.runTimes = inp;
                            Evolution.run();
                            JOptionPane.showMessageDialog(WMain.DMain,
                                    "Collecting statistics process is over");
                            Transform.transformFSM(new File( EnvDescription.FSMXMLfilename).getAbsolutePath()
                                   ,
                                    "tempFSM.html");
                            File tempFSMFilename = new File("tempFSM.html");

                            JEditorPane mainPanel = POpenFilePanel
                                    .showPanel(tempFSMFilename);
                            if (mainPanel != null) {
                                WMain.evolver.mainPanel
                                        .setDocument(new javax.swing.text.html.HTMLDocument());
                                WMain.evolver.mainPanel
                                        .setContentType(mainPanel
                                                .getContentType());
                                WMain.evolver.mainPanel.setDocument(mainPanel
                                        .getDocument());
                                WMain.evolver.mainPanel.repaint();

                            }

                            File traceFilename = new File(EnvDescription.traceFileName);
                            JEditorPane logPanel = POpenFilePanel
                                    .showPanel(traceFilename);
                            if (logPanel != null) {
                                WMain.evolver.bottomPanel
                                        .setDocument(new javax.swing.text.html.HTMLDocument());
                                WMain.evolver.bottomPanel
                                        .setContentType(logPanel
                                                .getContentType());
                                WMain.evolver.bottomPanel.setDocument(logPanel
                                        .getDocument());
                                WMain.evolver.bottomPanel.repaint();
                            }
                        }

                    }

                }
            }

        });
        add(bStat);

        bEvolve.setBounds(5, 5, 110, 30);
        bEvolve.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                Evolution.run();
                JOptionPane.showMessageDialog(WMain.DMain,
                        "The evolution is over with fitness "
                                + (Math.round(Evolution.solution.getFitness()
                                        / Enviroment.bestFitness * 100))
                                + "% during " + Evolution.solvedIn
                                + " generations");
                
       
                    File tempFSMFile = new File("tempFSM.html");
                    tempFSMFile.deleteOnExit();
                
                    // Write to temp file
                    Transform.transformFSM(EnvDescription.FSMXMLfilename,
                            tempFSMFile.getName() );
                    
                    JEditorPane mainPanel = POpenFilePanel
                            .showPanel(tempFSMFile);
     
                if (mainPanel != null) {
                    WMain.evolver.mainPanel
                            .setDocument(new javax.swing.text.html.HTMLDocument());
                    WMain.evolver.mainPanel.setContentType(mainPanel
                            .getContentType());
                    WMain.evolver.mainPanel
                            .setDocument(mainPanel.getDocument());
                    WMain.evolver.mainPanel.repaint();

                }

                
                
                File traceFilename = new File(EnvDescription.traceFileName);
                JEditorPane logPanel = POpenFilePanel.showPanel(traceFilename);
                if (logPanel != null) {
                    WMain.evolver.bottomPanel
                            .setDocument(new javax.swing.text.html.HTMLDocument());
                    WMain.evolver.bottomPanel.setContentType(logPanel
                            .getContentType());
                    WMain.evolver.bottomPanel.setDocument(logPanel
                            .getDocument());
                    WMain.evolver.bottomPanel.repaint();
                }
            }
        });
        add(bEvolve);

        this.setVisible(true);

    }

}
